package cn.com.bluemoon.daps.system.service.impl;

import cn.com.bluemoon.daps.system.dto.OperateLogPageDTO;
import cn.com.bluemoon.daps.system.entity.DapOperateLog;
import cn.com.bluemoon.daps.system.mapper.DapOperateLogMapper;
import cn.com.bluemoon.daps.system.service.DapOperateLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2021-12-29
 */
@Slf4j
@Service
public class DapOperateLogServiceImpl extends ServiceImpl<DapOperateLogMapper, DapOperateLog>
        implements DapOperateLogService {

    @Override
    public IPage<DapOperateLog> selectOperateLogPage(OperateLogPageDTO dto) {
        LambdaQueryWrapper<DapOperateLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(dto.getModuleName()), DapOperateLog::getModuleName, dto.getModuleName())
                .eq(StringUtils.isNotBlank(dto.getOpType()),DapOperateLog::getOpType, dto.getOpType())
                .eq(StringUtils.isNotBlank(dto.getOpStatus()), DapOperateLog::getOpStatus, dto.getOpStatus())
                .and(StringUtils.isNotBlank(dto.getKey()), w -> w.like(DapOperateLog::getModuleName, dto.getKey())
                .or().like(DapOperateLog::getOpDetail, dto.getKey()))
                .between(dto.getStartTime() != null && dto.getEndTime() != null, DapOperateLog::getOpTime, dto.getStartTime(), dto.getEndTime());
        wrapper.orderByDesc(DapOperateLog::getOpTime);
        IPage<DapOperateLog> logIPage = this.getBaseMapper().selectPage(new Page<DapOperateLog>
                (dto.getPageNum(), dto.getPageSize()), wrapper);
        return logIPage;
    }

    @Override
    public Boolean insertOperateLog(DapOperateLog operateLog) {
        return this.save(operateLog);
    }
}
